import 'dart:async';

import 'package:cunyutech_flutter_ui/src/theme/main.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';

typedef Future<bool> OnPreviewTap();

class CountDown extends StatefulWidget {
  final int countdown;
  final String inactiveText;
  final String reInactiveText;
  final String activePrefixText;
  final bool disabled;
  final double height;
  final double width;
  final EdgeInsetsGeometry margin;
  final EdgeInsetsGeometry padding;
  final Decoration decoration;
  final OnPreviewTap onPreviewTap; 
  final Alignment alignment;

  CountDown({ 
    Key key, 
    this.countdown = 60, 
    this.inactiveText, 
    this.reInactiveText, 
    this.activePrefixText, 
    this.disabled = false, 
    this.height, 
    this.width, 
    this.margin, 
    this.padding,
    this.decoration, 
    this.onPreviewTap,
    this.alignment,
  }) : super(key: key);

  _CountDownState createState() => _CountDownState();
}

enum CountDownStatus {
  inactive,
  active,
  reInactive,
}

class _CountDownState extends State<CountDown> {
  Timer timer;
  int secound;
  CountDownStatus status;

  buildCountDownText() {
    var cs = CYThemeData.of(context);
    String inactiveText = widget.inactiveText;
    String reInactiveText = widget.reInactiveText;
    String activePrefixText = widget.activePrefixText;
    switch (status) {
      case CountDownStatus.inactive:
        return Text(inactiveText ??= "获取验证码",style: TextStyle(color: cs.primaryForeColor, fontSize: CYFontSize.Large ));
        break;
      case CountDownStatus.active:
        return Text("${activePrefixText ??= "已发送"}${secound}s",style: TextStyle(color: cs.primaryForeColor));
        break;
      case CountDownStatus.reInactive:
        return Text(reInactiveText ??= "重新发送",style: TextStyle(color: cs.primaryForeColor, fontSize: CYFontSize.Large));
        break;
      default:
        break;
    }
    return Text("获取验证码",style: TextStyle(color: cs.primaryForeColor));
  }

  _startTimer() {
    timer?.cancel();
    secound = widget.countdown;
    status = CountDownStatus.active;
    timer = Timer.periodic(Duration(seconds: 1), (_timer){
      if (secound == 0) {
        status = CountDownStatus.reInactive;
      }
      secound -= 1;
      setState(() {});
    });
    setState(() {});
  }

  @override
  void dispose() { 
    timer?.cancel();
    super.dispose();
  }
  
  @override
  Widget build(BuildContext context) {
    return InkWell(
       child: Container(
         alignment: widget.alignment ?? Alignment.center,
         height: widget.height,
         width: widget.width,
         margin: widget.margin,
         padding: widget.padding,
         decoration: widget.decoration,
         child: buildCountDownText(),
       ),
       onTap: widget.disabled || <CountDownStatus>[CountDownStatus.active].contains(status)  ? null : () async {
         if(widget.onPreviewTap!= null && !(await widget.onPreviewTap())) {
          return;
         }
        _startTimer();
       },
    );
  }
}